package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.HouseService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/house")
public class HouseController {

    @Autowired
    HouseService houseService;


    @ApiOperation(value = "新增房型", notes = "新增房型")
    @ApiImplicitParam(name = "house", value = "酒店对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(String param) {
        ParamData<House> g = JSONObject.parseObject(param, new TypeReference<ParamData<House>>() {});
        Return ret = new Return();
        if (houseService.insert(g.getData())>0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改房型", notes="修改房型")
    @ApiImplicitParam(name = "house", value = "房型对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(String param){
        ParamData<House> g = JSONObject.parseObject(param, new TypeReference<ParamData<House>>() {});
        Return ret=new Return();
        if( houseService.update(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取房型列表", notes = "获取房型列表")
    @GetMapping
    public Return<List<House>> query(PageInfo pageInfo) {
        Return<List<House>> ret = new Return<>();
        List<House> houses = houseService.query();
        ret.setData(houses);
        return ret;
    }
    @ApiOperation(value = "获取房型列表", notes = "根据酒店ID获取房型列表")
    @GetMapping(value = "/byHotel/{hotelId}")
    public Return<List<House>> query(PageInfo pageInfo,@PathVariable(value = "hotelId") String hotelId) {
        Return<List<House>> ret = new Return<>();
        List<House> houses = houseService.queryByHotel(hotelId);
        ret.setData(houses);
        ret.getCommon().setRet(true);
        return ret;
    }

    @ApiOperation(value = "获取房型", notes = "获取房型")
    @ApiImplicitParam(name = "goodsId", value = "房型ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{goodsId}")
    public Return<House> getHotelById(@PathVariable(value = "goodsId") String goodsId) {
        Return<House> ret = new Return<>();
        House house = houseService.queryById(goodsId);
        ret.setData(house);
        return ret;
    }


    @ApiOperation(value = "删除房型", notes = "删除房型")
    @ApiImplicitParam(name = "goodsIds", value = "房型ID", required = true,  dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{goodsIds}")
    public Return<House> delete(@PathVariable(value = "goodsIds") List<String> goodsIds) {
        Return<House> ret = new Return<>();
        if(houseService.delete(goodsIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }


}
